package com.bridgeintelligent.tag.customer.track;

import cn.hutool.json.JSONUtil;
import com.bridgeintelligent.tag.customer.model.CustomerModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "客群追踪服务接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/track")
@Slf4j
public class TrackController {

    private ITrackService trackService;

    @ApiOperation("新增客群追踪")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "trackInfo", value = "追踪客群配置信息")
    })
    @PostMapping("/add")
    public TrackResp addTrackInfo(@RequestBody TrackInfo trackInfo) {
        try {
            if (!trackService.isUsed(trackInfo.getId(),trackInfo.getCustomerId())){
                return TrackResp.error("客群已被别的追踪使用");
            }
            trackService.addTrack(trackInfo);
            return TrackResp.ok();
        } catch (Exception e) {
            log.error("客群追踪添加失败：{}:",e);
            return TrackResp.error("客群追踪添加失败!"+e.getMessage());
        }
    }

    @ApiOperation("修改客群追踪")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "trackInfo", value = "客群追踪配置信息")
    })
    @PostMapping("/update")
    public TrackResp updateTrack(@RequestBody TrackInfo trackInfo) {
        try {
            if (!trackService.isUsed(trackInfo.getId(),trackInfo.getCustomerId())){
                return TrackResp.error("客群已被别的追踪使用");
            }
            trackService.updateTrack(trackInfo);
            return TrackResp.ok();
        } catch (Exception e) {
            log.error("客群追踪修改失败：{}:",e);
            return TrackResp.error("客群追踪修改失败!"+e.getMessage());
        }
    }

    @ApiOperation("根据追踪ID查看客群追踪信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "客群追踪ID")
    })
    @GetMapping("/findById")
    public TrackResp findById(String id) {
        try {
            TrackInfo data = trackService.findById(id);
            return TrackResp.ok(data);
        }catch (Exception e){
            log.error("查看客群追踪信息失败：{}:",e);
            return TrackResp.error("查看客群追踪信息失败");
        }
    }

    @ApiOperation("根据客群ID查看客群追踪信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "customerId", value = "客群ID")
    })
    @GetMapping("/findByCustomerId")
    public TrackResp findByCustomerId(String customerId) {
        try {
            TrackInfo data = trackService.findByCustomerId(customerId);
            return TrackResp.ok(data);
        }catch (Exception e){
            log.error("查看客群追踪信息失败：{}:",e);
            return TrackResp.error("查看客群追踪信息失败");
        }
    }

    @ApiOperation("删除客群追踪信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "客群追踪ID",required = true)})
    @GetMapping("/del")
    public TrackResp del(String id) {
        try {
            trackService.delById(id);
            return TrackResp.ok();
        }catch (Exception e){
            log.error("删除客群追踪信息失败：{}:",e);
            return TrackResp.error("删除客群追踪信息失败");
        }
    }


    @ApiOperation("查看追踪客群列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "customerName", value = "客群名称",required = false),
            @ApiImplicitParam(paramType = "query", name = "timeType", value = "动静态标识",required = false),
            @ApiImplicitParam(paramType = "query", name = "trackCycle", value = "追踪周期",required = false),
            @ApiImplicitParam(paramType = "query", name = "trackUptStartTime", value = "监测更新开始时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "trackUptEndTime", value = "监测更新结束时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "createStartTime", value = "创建开始时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "createEndTime", value = "创建结束时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "trackStatus", value = "监测状态",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小",required = false)
    })
    @GetMapping("/findList")
    public TrackResp findList(String customerName,String timeType,String trackCycle,String trackUptStartTime,String trackUptEndTime,
                                 String createStartTime,String createEndTime,String trackStatus,Integer pageNum,Integer pageSize) {
        try {
            TrackPage<TrackInfo> data = trackService.findList(customerName,timeType,trackCycle,trackUptStartTime,trackUptEndTime,
                     createStartTime,createEndTime,trackStatus,pageNum,pageSize);
            return TrackResp.ok(data);
        }catch (Exception e){
            log.error("查看追踪客群列表失败：{}:",e);
            return TrackResp.error("查看追踪客群列表失败");
        }
    }

    @ApiOperation("客群追踪统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "queryCondition", value = "查询条件")
    })
    @PostMapping("/getTrackData")
    public TrackResp getTrackData(String queryCondition) {
        try {
            List<TrackQueryModel> trackQueryModels = JSONUtil.toList(queryCondition, TrackQueryModel.class);
            Map<String,List<DataModel>> resultMap = new LinkedHashMap<>();
            for (TrackQueryModel queryModel:trackQueryModels ) {
                try {
                    List<DataModel> data = trackService.getTrackData(queryModel);
                    resultMap.put(queryModel.getDimId(),data);
                }catch (Exception e){
                    log.error("获取标签："+queryModel.getDimId()+"的追踪数据异常",e);
                }
            }
            return TrackResp.ok(resultMap);
        }catch (Exception e){
            log.error("客群追踪统计数据失败：{}:",e);
            return TrackResp.error("客群追踪统计数据失败"+e.getMessage());
        }
    }

    @ApiOperation("查询客群追踪客群清单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "配置ID"),
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "索引ID"),
            @ApiImplicitParam(paramType = "query", name = "customerName", value = "客群名称")
    })
    @GetMapping("/getTrackCustomer")
    public TrackResp getTrackCustomer(String id,String indexId,String customerName) {
        try {
            List<CustomerModel> data = trackService.trackCustomerList(id,indexId, customerName);
            return TrackResp.ok(data);
        }catch (Exception e){
            log.error("查询客群追踪客群清单数据失败：{}:",e);
            return TrackResp.error("查询客群追踪客群清单数据失败");
        }
    }

    private List<TrackQueryModel> makeData(){
        List<TrackQueryModel> list = new ArrayList<>();

        TrackQueryModel model1 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("age")
                .setDimName("年龄");
        TrackQueryModel model2 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("gender_cd")
                .setDimName("性别");
        TrackQueryModel model3 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("open_acct_dt")
                .setDimName("开户日期");
        TrackQueryModel model4 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("open_acct_dt")
                .setDimName("开户日期");
        TrackQueryModel model5 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("cust_num")
                .setDimName("客群人数");

        TrackQueryModel model6 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("pb_accfund_area")
                .setDimName("公积金参缴地");
        TrackQueryModel model7 = new TrackQueryModel()
                .setTrackId("1")
                .setStartTime("2023-09-11")
                .setEndTime("2023-09-13")
                .setDimId("keeping_card_name")
                .setDimName("客户持有卡片");

        list.add(model1);
        list.add(model2);
        list.add(model3);
        list.add(model4);
        list.add(model5);
        list.add(model6);
        list.add(model7);
        return list;

    }

    @ApiOperation("客群追踪客群维度统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "trackId", value = "客群追踪ID"),
            @ApiImplicitParam(paramType = "query", name = "trackStartTime", value = "开始时间"),
            @ApiImplicitParam(paramType = "query", name = "trackEndTime", value = "结束时间"),
            @ApiImplicitParam(paramType = "query", name = "sortWord", value = "排序字段"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "升降序"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "数值型标签取值类型"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小")
    })
    @GetMapping("/getDimData")
    public TrackResp getDimData(String trackId,String trackStartTime,String trackEndTime,
                                String sortWord,Boolean sort,String type,Integer pageNum,Integer pageSize) {
        try {
            TrackPage<SortedModel> data = trackService.getDimData(trackId, trackStartTime, trackEndTime, sortWord, sort, type, pageNum, pageSize);
            return TrackResp.ok(data);
        }catch (Exception e){
            log.error("客群追踪客群维度统计数据失败：{}:",e);
            return TrackResp.error("客群追踪客群维度统计数据失败"+e.getMessage());
        }
    }
}